﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data;

namespace DVSProject.Controllers
{
    public class GenericReportViewerController : Controller
    {
        //
        // GET: /GenericReportViewer/

        public ActionResult Index()
        {
            return View();
        }

        public void ShowGenericRpt()
        {
            try
            {
                bool isValid = true;

                string strReportName = System.Web.HttpContext.Current.Session["ReportName"].ToString();    // Setting ReportName
                string strProduct = System.Web.HttpContext.Current.Session["rptProduct"].ToString();     // Setting FromDate 
                string strMonthShow = System.Web.HttpContext.Current.Session["rptMonthShow"].ToString(); 
                string strFromDate = System.Web.HttpContext.Current.Session["rptFromDate"].ToString();     // Setting FromDate 
                string strToDate = System.Web.HttpContext.Current.Session["rptToDate"].ToString();         // Setting ToDate    
               DataTable _dt = (DataTable)System.Web.HttpContext.Current.Session["rptSource"];

                if (string.IsNullOrEmpty(strReportName))
                {
                    isValid = false;
                }

                if (isValid)
                {
                    _dt.TableName = "dt_SumMeterByDate";
                    DataSet ds = new DataSet();
                    ds.Merge(_dt);
                     using (ReportClass rptH = new ReportClass())
                    {
                    ReportDocument rd = new ReportDocument();
                    string strRptPath = System.Web.HttpContext.Current.Server.MapPath("~/") + "Rpts//" + strReportName;
                  
                    rd.Load(strRptPath);
                    if (_dt != null && _dt.Rows.Count>0 )
                        rd.SetDataSource(ds);

                    if (!string.IsNullOrEmpty(strProduct))
                        rd.SetParameterValue("product", strProduct);
                    if (!string.IsNullOrEmpty(strMonthShow))
                        rd.SetParameterValue("monthshow", strMonthShow);
                    if (!string.IsNullOrEmpty(strFromDate))
                        rd.SetParameterValue("fromDate", strFromDate);
                    if (!string.IsNullOrEmpty(strToDate))
                        rd.SetParameterValue("toDate", strToDate);

                    //rd.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");
                    ////CrystalReportViewer1.ReportSource = rd;

                    //rd.ExportToHttpResponse(
                    //using (ReportClass rptH = new ReportClass())
                    //{

                    //    rptH.FileName = Server.MapPath("~/") + "//Rpts//rptSumMeterByDate.rpt";
                    //    rptH.Load();
                       
                        //rptH.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");
                    rd.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");
                    }


                    // Clear all sessions value
                    Session["ReportName"] = null;
                    Session["rptProduct"] = null;
                    Session["rptMonthShow"] = null;
                    Session["rptFromDate"] = null;
                    Session["rptToDate"] = null;
                    Session["rptSource"] = null;
                }
                else
                {
                    Response.Write("<H2>Nothing Found; No Report name found</H2>");
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
    }
}
